/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package deviceSyc;

import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.apache.log4j.BasicConfigurator;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.util.Date;
import java.util.Properties;

/**
 * This Example will demonstrate how to start and shutdown the Quartz scheduler
 * and how to schedule a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class MainTest {

	public void run(String cronStr, boolean startRightNow) throws Exception {
		Logger log = LoggerFactory.getLogger(MainTest.class);

		log.info("------- Initializing ----------------------");

		// First we must get a reference to a scheduler
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();

		log.info("------- Initialization Complete -----------");

		// computer a time that is on the next round minute
		Date runTime = evenMinuteDate(new Date());

		log.info("------- Scheduling Job  -------------------");

		// define the job and tie it to our HelloJob class
		JobDetail job = newJob(JSONAnalysis.class).withIdentity("job1", "group1").build();

		// Trigger the job to run on the next round minute
		Trigger trigger;
		if (!startRightNow) {
			trigger = newTrigger().withIdentity("trigger1", "group1")
					.withSchedule(CronScheduleBuilder.cronSchedule(cronStr)).build();
		} else {
			trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
		}
		// Tell quartz to schedule the job using our trigger
		sched.scheduleJob(job, trigger);
		log.info(job.getKey() + " will run at: " + runTime);

		// Start up the scheduler (nothing can actually run until the
		// scheduler has been started)
		sched.start();

		log.info("------- Started Scheduler -----------------");

		// wait long enough so that the scheduler as an opportunity to
		// run the job!
		log.info("------- Waiting 65 seconds... -------------");
		try {
			// wait 65 seconds to show job
			Thread.sleep(65L * 1000L);
			// executing...
		} catch (Exception e) {
			//
		}

		// shut down the scheduler
		// log.info("------- Shutting Down ---------------------");
		// sched.shutdown(true);
		// log.info("------- Shutdown Complete -----------------");
	}

	public static void main(String[] args) throws Exception {
		boolean startRightNow = false;
		if(args.length==1){
			if(args[0].equals("feback")){
				MQConsumer mqr = new MQConsumer();
				mqr.run();				
			}else{
				String cronStr = "";
				MainTest example = new MainTest();
				example.run(cronStr, true);
			}
		}else{
			String cronStr = "";
			String rootPath = System.getProperty("user.dir").replace("\\", "/");
			Properties pps = new Properties();
			pps.load(new FileInputStream(rootPath +"/res/configure.properties"));
			String unit = pps.getProperty("repeatUnit");
			String time = pps.getProperty("repeatTime");

			if (unit.equals("hour")) {
				if (Integer.valueOf(time) > 0 && Integer.valueOf(time) < 24) {
					cronStr = "0 0 */" + time + " * * ?";
				}
			} else if (unit.equals("day")) {
				if (Integer.valueOf(time) > 0 && Integer.valueOf(time) < 31) {
					cronStr = "0 0 1 */" + time + " * ?";
				}
			}else if (unit.equals("minute")) {
				if (Integer.valueOf(time) > 0 && Integer.valueOf(time) < 60) {
					cronStr = "0 */" + time + " * * * ?";
				}
			} else {
				System.out.println("请检查configure.properties文件参数是否有误");
				return;
			}
			System.out.println("cron表达式：" + cronStr);
			BasicConfigurator.configure();
			MainTest example = new MainTest();
			example.run(cronStr, startRightNow);
		}
	}

}
